﻿using AutoMapper;
using FoodModel;
using J.FoodManageSet.Application.Command.GroupingCommand;
using J.FoodRepository.Impl;
using J.FoodRepository.InterFace;
using MediatR;

namespace J.FoodManageSet.Application.CommandHandler.GroupingCommandHandler
{
    public class GroupingAddCommandHandler : IRequestHandler<GroupingAddCommand, int>
    {
        private readonly IGroupingRepository _groupingRepository;
        private readonly IMapper _mapper;

        public GroupingAddCommandHandler(IGroupingRepository groupingRepository,IMapper mapper)
        {
            _groupingRepository = groupingRepository;
           _mapper = mapper;
        }

        public async Task<int> Handle(GroupingAddCommand request, CancellationToken cancellationToken)
        {
            int count = 0;
            if (request.GroupingId == 0)
            {
                GroupingModel model = _mapper.Map<GroupingModel>(request);
                count = await _groupingRepository.GroupingAdd(model);
            }
            else
            {
                GroupingModel model = _mapper.Map<GroupingModel>(request);
                count = await _groupingRepository.GroupingUpDate(model);
            }
            return count;
        }
    }
}
